using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;

[Serializable]
public class ChangeColorEffectComponent : TakeDamageEffectBaseComponent
{
    public List<Renderer> renderers;
    public List<int> materialIndexs;
    public Color TakeDamageColor;
    private List<Color> InitColor = new List<Color>();
    [Header("插值效果")]
    public bool IsLerp; // 是否使用插值效果
    public float LerpTime;
    public float ChangeColorCoolDown; //闪红的冷却时间，可以为0
    private float TimeDownIntervel;
    private bool IsCanPlay = true; // 是否可以播放特效
    private string ColorPropertyName;

    public override void OnAwake()
    {
        base.OnAwake();

        // 初始化颜色列表
        for (int i = 0; i < renderers.Count; i++)
        {
            if (!renderers[i].materials[materialIndexs[i]].HasProperty("_BaseColor"))
            {

                InitColor.Add(renderers[i].materials[materialIndexs[i]].GetColor("_Color"));
                ColorPropertyName = "_Color";
            }
            else
            {
                InitColor.Add(renderers[i].materials[materialIndexs[i]].GetColor("_BaseColor"));
                ColorPropertyName = "_BaseColor";
            }
        }
    }

    public override void OnUpdate()
    {
        base.OnUpdate();
        if (!IsCanPlay)
        {
            TimeDownIntervel += Time.deltaTime;
            if (TimeDownIntervel >= ChangeColorCoolDown)
            {
                IsCanPlay = true;
                TimeDownIntervel = 0;
            }
        }
    }
    public override void TakeDamageEffectPlay(GameObject AttackObject)
    {
        base.TakeDamageEffectPlay(AttackObject);
        if (renderers.Count == 0)
        {
            return;
        }
        if (!IsCanPlay)
        {
            return;
        }
        IsCanPlay = false;

        for (int i = 0; i < renderers.Count; i++)
        {
            if (renderers[i] != null)
            {
                // if (!Application.isEditor) {
                if (IsLerp)
                {
                    int temp = i;
                    renderers[temp].materials[materialIndexs[temp]].DOKill();
                    Color TargetColor = TakeDamageColor;
                    Color CurrentColor = InitColor[temp];
                    DOTween.To(
                    () => CurrentColor,
                    x =>
                    {
                        CurrentColor = x;
                        renderers[temp].materials[materialIndexs[temp]].SetColor(ColorPropertyName, CurrentColor);
                    },
                        TargetColor,
                        LerpTime / 2
                    ).OnComplete(() =>
                    {
                        DOTween.To(() => CurrentColor, x =>
                        {
                            CurrentColor = x;
                            renderers[temp].materials[materialIndexs[temp]].SetColor(ColorPropertyName, CurrentColor);
                        },
                        InitColor[temp],
                        LerpTime / 2);
                    });
                }
                else
                {
                    int temp = i;
                    ColorChange(temp);
                }
            }
        }


    }

    public async void ColorChange(int tempIndex)
    {
        renderers[tempIndex].materials[materialIndexs[tempIndex]].SetColor(ColorPropertyName, TakeDamageColor);
        // 直接改变颜色
        await Task.Delay((int)(LerpTime * 1000));
        if (!Application.isPlaying)
        {
            return;
        }
        renderers[tempIndex].materials[materialIndexs[tempIndex]].SetColor(ColorPropertyName, InitColor[tempIndex]);
    }
}
